package com.faneui.transfer.gateway.convert;

import java.io.IOException;
import java.util.List;

import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;

/**
 * 自定义消息转换器
 * 
 * @author wangyifeng
 *
 */
public class ExingcaiHttpMessageConvert implements HttpMessageConverter {
	/**
	 * fastJsonHttpMessageConverter
	 */
	private HttpMessageConverter<Object> fastJsonHttpMessageConverter;

	/**
	 * mappingJackson2HttpMessageConverter
	 */
	private HttpMessageConverter<Object> mappingJackson2HttpMessageConverter;

	public ExingcaiHttpMessageConvert(HttpMessageConverter<Object> fastJsonHttpMessageConverter,
			HttpMessageConverter<Object> mappingJackson2HttpMessageConverter) {
		this.fastJsonHttpMessageConverter = fastJsonHttpMessageConverter;
		this.mappingJackson2HttpMessageConverter = mappingJackson2HttpMessageConverter;
	}

	
	public boolean canRead(Class clazz, MediaType mediaType) {
		return mappingJackson2HttpMessageConverter.canRead(clazz, mediaType);
	}


	public boolean canWrite(Class clazz, MediaType mediaType) {
		return fastJsonHttpMessageConverter.canWrite(clazz, mediaType);
	}

	
	public List getSupportedMediaTypes() {
		return mappingJackson2HttpMessageConverter.getSupportedMediaTypes();
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public Object read(Class clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
		return mappingJackson2HttpMessageConverter.read(clazz, inputMessage);
	}

	public void write(Object t, MediaType contentType, HttpOutputMessage outputMessage)
			throws IOException, HttpMessageNotWritableException {
		fastJsonHttpMessageConverter.write(t, contentType, outputMessage);

	}

}